package com.alliky.classify.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alliky.classify.BR;
import com.alliky.classify.R;
import com.alliky.classify.databinding.FragmentClassifyBinding;
import com.alliky.classify.ui.viewmodel.ClassifyViewModel;
import com.alliky.core.base.BaseFragment;
import com.alliky.core.router.RoutePath;

@Route(path = RoutePath.Classify.FRAGMENT_CLASSIFY)
public class ClassifyFragment extends BaseFragment<FragmentClassifyBinding, ClassifyViewModel> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_classify;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        //通过binding拿到toolbar控件, 设置给Activity
        ((AppCompatActivity) getActivity()).setSupportActionBar(binding.include.toolbar);
        //初始化标题
        mViewModel.initToolbar();
        mViewModel.getCategoryData();
        mViewModel.getGoodsData();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        mViewModel.uc.finishRefresh.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                binding.refreshLayout.finishRefresh();
            }
        });
        mViewModel.uc.finishLoadMore.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                binding.refreshLayout.finishLoadMore();
            }
        });
    }
}
